#include "main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：参数初始化
--------------------------------------------------------------------------------
*/
int init_value(void)
{
	int ret = 0;
	char value[128];

	memset(Server, 0, sizeof(Server));
	memset(value, 0, sizeof(value));
	ret = get_value("appctrl.@appctrl[0].server", &value);
	if (ret == 1 && strlen(value))
		memcpy(Server, value, sizeof(Server));
	else
		memcpy(Server, "61.153.186.12", sizeof(Server));

	memset(value, 0, sizeof(value));
	ret = get_value("appctrl.@appctrl[0].port", &value);
	if (ret == 1 && strlen(value))
		Port = atoi(value);
	else
		Port = 9001;

	memset(value, 0, sizeof(value));
	ret = get_value("appctrl.@appctrl[0].interval", &value);
	if (ret == 1 && strlen(value))
		HeartBeat = atoi(value);
	else
		HeartBeat = 1;

	memset(DiskPath, 0, sizeof(DiskPath));
	shell_cmd("cat /proc/mounts | grep '/dev/mmcblk' | awk '{print $2}'", DiskPath);
	//shell_cmd("cat /proc/mounts | grep -w 'tmpfs' | awk '{print $2}'", DiskPath);

	memset(RouteID, 0, sizeof(RouteID));
	memset(value, 0, sizeof(value));
	ret = get_value("web.system.id", &value);
	if (ret == 1 && strlen(value))
		memcpy(RouteID, value, sizeof(RouteID));

	memset(ServerID, 0, sizeof(ServerID));
	memset(value, 0, sizeof(value));
	ret = get_value("appctrl.@appctrl[0].serverid", &value);
	if (ret == 1 && strlen(value))
		memcpy(ServerID, value, sizeof(ServerID));
	else
		memcpy(ServerID, "d5c0bfaca2934f22bc3f8ff4b7b0dc97", sizeof(ServerID));

	memset(FCNIP, 0, sizeof(FCNIP));
	system_fcnip();

	printf("### RouteID ### %s \n", RouteID);
	printf("### ServerID ### %s \n", ServerID);
	printf("### FCNIP ### %s \n", FCNIP);
}

/* 
--------------------------------------------------------------------------------
	函数功能：执行shell命令,返回相关结果
--------------------------------------------------------------------------------
*/
int shell_cmd(char *cmd, char *value)
{
	FILE *fp = NULL;
	char buf[256];

	if ((fp = popen(cmd, "r")) == NULL)
	{
		OUT_DEBUG(DEBUG, "cmd, %s ERROR !\n", cmd);
		return -1;
	}
	else
	{
		if (fgets(buf, sizeof(buf), fp) != NULL)
		{
			if (buf[strlen(buf) - 1] == '\r' || buf[strlen(buf) - 1] == '\n')
				buf[strlen(buf) - 1] = '\0';
			memcpy(value, buf, strlen(buf));
		}
		pclose(fp);
		return 1;
	}
}


char *shell_cmd_change(char *cmd)
{
	char *result = NULL;
	FILE *fp = NULL;
	char buf[102400];

	if ((fp = popen(cmd, "r")) == NULL)
	{
		OUT_DEBUG(DEBUG, "cmd, %s ERROR !\n", cmd);
		return result;
	}
	else
	{
		if (fgets(buf, sizeof(buf), fp) != NULL)
		{
			if (buf[strlen(buf) - 1] == '\r' || buf[strlen(buf) - 1] == '\n')
			{
				buf[strlen(buf) - 1] = '\0';
			}
			result = charCopy(buf, strlen(buf));
		}
		pclose(fp);
		return result;
	}
}

/* 
--------------------------------------------------------------------------------
 函数功能：Debug打印
--------------------------------------------------------------------------------
*/
int cwprintf(int level, const char *fmt, ...)
{
	char buf[1024 * 10] = "";
	va_list args;
	va_start(args, fmt);
	vsprintf(buf, fmt, args);
	printf("%s\n", buf);
	return 0;
}

void run_total()
{
	// pthread_t pt_client;
	// pthread_create(&pt_client, NULL, (void *)tcp_client, NULL);
	pthread_t pt_router_server;
	pthread_create(&pt_router_server, NULL, (void *)router_server, NULL);
}

/* 
--------------------------------------------------------------------------------
	函数功能：主函数
--------------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
	init_value();

	OUT_DEBUG(DEBUG, "Version: 0X%02X ", MsgVer);
	run_total();

	// 捕获数据包
	msg();

	return 0;
}
